package com.example.schedulecard;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.schedulecard.DataTransfer.ScheduleNameTransfer;
import com.example.schedulecard.Databases.CourseDatabaseOption;
import com.example.schedulecard.Databases.ScheduleFestOption;
import com.example.schedulecard.Entity.Schedule;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView schedule_name;
    private ImageView add_schedule;
    private List<Schedule> list;

    private SQLiteDatabase sqLiteDatabase;
//    存储课程表名称信息
    private String[] schedule_mes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Init();
    }

    private void Init() {
        schedule_name = (ListView) findViewById(R.id.schedulelist);
        add_schedule = (ImageView) findViewById(R.id.add_schedule);
        add_schedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AddSchedule.class);
                startActivity(intent);
            }
        });

        sqLiteDatabase = ScheduleFestOption.CreateScheduletable(MainActivity.this)
                .getWritableDatabase();
//        从数据库获取课程表清单
        list = ScheduleFestOption.querySchedule(sqLiteDatabase);
        schedule_mes = new String[list.size()];
        for (int i = 0; i < list.size(); ++i) {
            schedule_mes[i] = list.get(i).getSchedulename();
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>
                (this, android.R.layout.simple_list_item_1, schedule_mes);
        schedule_name.setAdapter(adapter);
//        每个项目添加单击事件
        schedule_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ScheduleNameTransfer.scheduleinfo=list.get(i);
                Intent intent = null;
                if (ScheduleNameTransfer.scheduleinfo.getWorksum() == 0){
                    intent = new Intent(MainActivity.this, UpdataScheduleInfo.class);
                } else {
//                跳转
                    intent = new Intent(MainActivity.this, ScheduleCardHome.class);
//                  Bundle bundle = new Bundle();
//                  bundle.putSerializable("schedule",name);
//                  intent.putExtras(bundle);
                }
                startActivity(intent);
            }

        });
        schedule_name.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Schedule delname = list.get(i);
                new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
//                        设置提示信息
                        .setMessage("确定删除该课表吗？")
//                        设置确定点击事件
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                SQLiteDatabase sqLiteDatabase = ScheduleFestOption
                                        .CreateScheduletable(MainActivity.this)
                                        .getWritableDatabase();
                                SQLiteDatabase sqLiteDatabase1 = CourseDatabaseOption
                                        .CreateCoursetable(MainActivity.this)
                                        .getWritableDatabase();
//                                删除课表内的课程
                                int n = CourseDatabaseOption.deleteschedule(sqLiteDatabase1, delname);
//                                删除课表
                                int m = ScheduleFestOption.deleteSchedule(sqLiteDatabase, delname);
                                if (n >= 0 && m > 0) {
                                    Toast.makeText(MainActivity.this, "删除成功！\n删除" + m + "份课表，包含"
                                            + n + "门课", Toast.LENGTH_LONG).show();
//                                    刷新页面
                                    refresh();
                                } else {
                                    Toast.makeText(MainActivity.this, "删除失败！", Toast.LENGTH_LONG).show();
                                }
                            }
                        })
//                        设置取消点击事件
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).show();
                return true;
            }
        });
    }

//    刷新页面方法
    private void refresh() {
        finish();
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
    }
}
